package huang.course.jan21;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
	Connection con=null;
	BaseDao basedao=new BaseDao();
	PreparedStatement ps=null;
	ResultSet rs;
		public User getUserbyName(String name) throws SQLException{
			con=basedao.getCon();
			String sql="select * from user where username=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, name);
			rs=ps.executeQuery();
			String pwd=null;
			int id=0;
			while(rs.next()){
				pwd=rs.getString("pwd");
				id=rs.getInt("id");
			}		
			User u =new User(id,name,pwd);
			close();
			return u;
		}
		public void close() throws SQLException{
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
		}
		
		
		public boolean risgter(String name,String pwd) throws SQLException{
			boolean result=false;
			con=basedao.getCon();
			String sql="insert user (username,pwd) value(?,?)";
			ps=con.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, pwd);
			if(ps.executeUpdate()>0){
				result=true;	
			}
			close();
			return result;
		}
	public static void main(String[] args) {
		UserDao ud=new UserDao();
		try {
			User u =ud.getUserbyName("huang");
			System.out.println(u.getPwd());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
